package com.summer_administration.mapper;

import com.summer_administration.pojo.Material;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface MaterialMapper {
    @Insert("INSERT INTO materials(met_name, met_num, met_attr, remain_num, create_user, create_time, update_time) " +
            "VALUES(#{metName}, #{metNum}, #{metAttr}, #{remainNum}, #{createUser}, #{createTime}, #{updateTime})")
    void add(Material material);

    @Select("SELECT * FROM materials WHERE create_user = #{userId}")
    List<Material> listByUser(Integer userId);

    @Update("UPDATE materials SET " +
            "met_name = #{metName}, " +
            "met_num = #{metNum}, " +
            "met_attr = #{metAttr}, " +
            "remain_num = #{remainNum}, " +
            "update_time = #{updateTime} " +
            "WHERE id = #{id}")
    void update(Material material);

    @Delete("DELETE FROM materials WHERE id = #{id}")
    void delete(Integer id);

    @Select("SELECT * FROM materials WHERE id = #{id}")
    Material getById(Integer id);

    @Select("SELECT COUNT(*) FROM materials WHERE met_name = #{metName}")
    boolean existsByName(String metName);

    @Update("UPDATE materials SET remain_num = remain_num + #{delta} WHERE met_name = #{name}")
    int updateStock(String name, int delta);
}